package com.woniu.controller;

import com.woniu.entity.PageResult;
import com.woniu.entity.Result;

import com.woniu.pojo.User;
import com.woniu.service.IUserService;
import com.woniu.vo.QueryUserVo;
import com.woniu.vo.UserDetailVo;
import com.woniu.vo.UserInformationVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author doujiao
 * @since 2022-10-14
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户模块")
public class UserController extends BaseController{

    @Resource
    private IUserService userService;

    /**
     * 根据用户条件查询所有关于用户的id
     */
    @PostMapping("/search/{currentPage}/{pageSize}")
    @ApiOperation("根据用户条件查询所有关于用户的id")
    public Result queryByPage(@PathVariable Long currentPage, @PathVariable Long pageSize,
                              @RequestBody QueryUserVo userVo){
          PageResult pageResult= userService.queryByPage(currentPage,pageSize,userVo);
          return toDataResult(pageResult);
    }
    /**
     * 根据userid查询user的详情信息
     */
    @GetMapping("/detail/{userId}")
    @ApiOperation("根据userid查询user的详情信息")
    public Result queryDetailByUserId(@PathVariable Long userId){
       UserDetailVo userDetailVo= userService.queryDetailByUserId(userId);
       return toDataResult(userDetailVo);
    }

    /**
     * 编辑
     */
    @PutMapping
    @ApiOperation("编辑")
    public Result edit(@RequestBody UserDetailVo userDetailVo){
       Boolean flag= userService.edit(userDetailVo);
       return toResult(flag);
    }
    /**
     * 增加
     */
    @PostMapping
    @ApiOperation("增加")
    public Result add(@RequestBody User user){
        boolean save = userService.add(user);
        return toResult(save);
    }
    /**
     * 修改
     */
    @DeleteMapping("/{id}")
    @ApiOperation("根据id删除")
    public Result removeById(@PathVariable Long id){
       Boolean flag= userService.removeByIdPlus(id);
       return toResult(flag);
    }

    @ApiOperation("根据id查询服务管理的居民信息")
    @GetMapping("/people/{userId}")
    public Result queryById(@PathVariable Long userId) throws ParseException {
        UserInformationVo userInformationVo = userService.getPeopleById(userId);
        return toDataResult(userInformationVo);
    }

    @ApiOperation("修改用户发布状态")
    @PutMapping("/sta/{userId}")
    public Result getByValAndCourseId(@PathVariable Long userId){
        boolean flag = userService.saveStatus(userId);
        return toResult(flag);
    }

    @ApiOperation("查询所有用户")
    @GetMapping
    public Result getAllUser(){
        List<User> userList = userService.list();
        return toDataResult(userList);
    }
}
